package com.aglhz.nature.adapter;

import java.util.List;


import com.aglhz.nature.R;
import com.aglhz.nature.modle.item.SearchData;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.view.SecondHomeWebActivity;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;




public class SearchRecommedAdapter extends BaseAdapter{
	public List<ShopListData>mData;
    public Context context;
	public SearchRecommedAdapter(List<ShopListData> mData, Context context) {
		super();
		this.mData = mData;
		this.context = context;
	}
	@Override
	public int getCount() {
		return mData.size();
	}
	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}
	@Override
	public long getItemId(int position) {
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		 ViewHolder viewHolder; 
			if(null==convertView){			
				  convertView = LayoutInflater.from(context).inflate(R.layout.item_search_regridview, null); 
				  viewHolder = new ViewHolder();
				  viewHolder.re_image=(ImageView) convertView.findViewById(R.id.re_image);
				  viewHolder.re_name=(TextView) convertView.findViewById(R.id.re_name);
				  viewHolder.linearlayout=(LinearLayout) convertView.findViewById(R.id.linearlayout);
				  viewHolder.re_nomey=(TextView) convertView.findViewById(R.id.re_nomey);
				
				  viewHolder.linearlayout.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent=new Intent(context,SecondHomeWebActivity.class);
						intent.putExtra("SearchRecommed",mData.get(position).getLink());
						Log.e("SearchRecommed",mData.get(position).getLink());
						context.startActivity(intent);
						
					}
				});
				  convertView.setTag(viewHolder); 
			}else{
				viewHolder = (ViewHolder) convertView.getTag(); 
			}
			
	   
			//设置图片
			
			  String imgUrl = mData.get(position).getPictureURL();
			//picasso加载网络图片
			   Picasso.with(context)  
			   .load(imgUrl) 
			   .resize(getScreenWidth()/2, getScreenWidth()/2)
			   .into(viewHolder.re_image);
			//设置文字
			   viewHolder.re_name.setText( mData.get(position).getName());
	
			   viewHolder.re_nomey.setText("￥：" +mData.get(position).getPrice());
			
//			int screenWidth = getScreenWidth();
//			GridView.LayoutParams p=new GridView.LayoutParams(screenWidth,screenWidth);
//			convertView.setLayoutParams(p);

			return convertView;
		}
		class ViewHolder 
		{  
		    public ImageView re_image;
		    public TextView re_name;
		    public LinearLayout linearlayout;
		    public TextView re_nomey;

		} 
		
		/**
		 * 获取屏幕宽度
		 * 
		 * @return
		 */
		protected int getScreenWidth() {
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			return display.getWidth();
		}
    
    
}
